﻿using FirstFloor.ModernUI.Presentation;
using FirstFloor.ModernUI.Windows.Controls;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Microsoft.Practices.Unity;
using appCtx = EasyInvoice.ApplicationContext.AppContext;

namespace EasyInvoice.WPF.ViewModel
{
    public class CreateInvoiceViewModel:NotifyPropertyChanged
    {
        private Services.InvoicesService _serviceInvoices;

        private DateTime _StartDate;
        private DateTime _EndDate;
        private ClientViewModel _Client;
        private ClientViewModel _Seller;
        private Int64 _number;

        private int _invoicingDay = Properties.Settings.Default.InvoicingDate;
        private ObservableCollection<InvoiceDetailViewModel> _details = new ObservableCollection<InvoiceDetailViewModel>();

        public DateTime StartDate
        {
            get
            {
                return this._StartDate;
            }
            set
            {
                this._StartDate = value;
                OnPropertyChanged("StartDate");
            }
        }

        public DateTime EndDate
        {
            get
            {
                return this._EndDate;
            }
            set
            {
                this._EndDate = value;
                OnPropertyChanged("EndDate");
            }
        }

        public ClientViewModel Client
        {
            get
            {
                return this._Client;
            }
            set
            {
                this._Client = value;
                OnPropertyChanged("Client");
            }
        }

        public ClientViewModel Seller
        {
            get
            {
                return this._Seller;
            }
            set
            {
                this._Seller = value;
                OnPropertyChanged("Seller");
            }
        }

        public ObservableCollection<InvoiceDetailViewModel> Details
        {
            get
            {

                //if (this._details.Count == 0)
                //    this._details = GetDetails();
                //foreach (InvoiceDetailViewModel detail in _details)
                //    detail.Index = _details.IndexOf(detail) + 1;
                return this._details;
            }
            set
            {

                this._details = value;
                //this.OnPropertyChanged("Details");
            }
        }

        public CreateInvoiceViewModel()
        {
            this._serviceInvoices = appCtx.Instance.IoC.Resolve<Services.InvoicesService>();
            var invoices = _serviceInvoices.GetAll();
            if (invoices.Count() > 0)
            {
                this._StartDate = invoices.Max(m => m.Date);
            }
            else
            {
                this._StartDate = new DateTime(DateTime.Today.Year, DateTime.Today.Month, _invoicingDay);
            }
            this._EndDate = new DateTime(DateTime.Today.Year, DateTime.Today.Month, _invoicingDay);
            var invoice = new InvoiceViewModel(invoices.SingleOrDefault(m => m.Date == this._StartDate));
            Client = invoice.Client;
            Seller = invoice.Seller;
            _number = !string.IsNullOrWhiteSpace(invoice.Number)?Convert.ToInt64(invoice.Number):0;
        }


        //for opening up the Add Client window
        private ICommand startCommand;

        public ICommand StartCommand
        {
            get
            {
                if (startCommand == null)
                {
                    startCommand = new Common.CommandBase(
                        i => this.StartInvoicing(),
                        i => ((UserViewModel)Properties.Settings.Default.AuthenticatedUser) != null ? 
                                ((UserViewModel)Properties.Settings.Default.AuthenticatedUser).AddInvoices : 
                                false);
                }
                return startCommand;
            }
        }

        private void StartInvoicing()
        {
            var invoice = new InvoiceViewModel((_serviceInvoices.GetAll()).SingleOrDefault(m => m.Date == this._StartDate));
            _number = Convert.ToInt64(string.IsNullOrWhiteSpace(invoice.Number)?"0":invoice.Number);
            DateTime start = this._StartDate.AddMonths(1);
            _number++;
            while (start < this.EndDate)
            {
                DateTime invoiceDate = start;
                while (invoiceDate.DayOfWeek == DayOfWeek.Saturday || invoiceDate.DayOfWeek == DayOfWeek.Sunday)
                    invoiceDate=invoiceDate.AddDays(1);

                InvoiceViewModel newInvoice = new InvoiceViewModel()
                                                    {
                                                        Number = _number.ToString(),
                                                        Date = invoiceDate,
                                                        Client = _Client,
                                                        Seller = _Seller,
                                                    };
                foreach (InvoiceDetailViewModel detail in Details)
                {
                    newInvoice.Details.Add( 
                        new InvoiceDetailViewModel()
                            { 
                                Description = string.Format("{0} {1} {2}", detail.Description, invoiceDate.ToString("MMMM"), invoiceDate.Year.ToString()),
                                Price = detail.Price,
                                Units = detail.Units
                            }
                            );
                }

                newInvoice.ShowSaveCommand.Execute(null);
                //new InvoiceDocument(invoice, string.Format("{0}-{1}.pdf", invoice.Number, invoice.Date.ToString("yyyy-MM-dd")));

                start = start.AddMonths(1);
                _number++;
            }

            ModernDialog.ShowMessage("Invoices successfully generated.", "Bulk Invoice Generation", System.Windows.MessageBoxButton.OK);
        }
    }
}
